package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.maindata.domain.CommonClassify;
import com.qijian.maindata.domain.query.CommonClassifyQuery;
import com.qijian.maindata.service.ICommonClassifyService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用树形分类Controller
 *
 * @author qijian
 * @date 2021-12-23
 */
@Api(tags = "通用树形分类")
@RestController
@RequiredArgsConstructor
@RequestMapping("/tools/commonClassify")
public class CommonClassifyController extends BaseController {

    private final ICommonClassifyService commonClassifyService;

    /**
     * 查询通用树形分类列表
     */
//    @PreAuthorize("@ss.hasPermi('tools:commonClassify:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Validated CommonClassifyQuery query) {
        if ("knifeArea".equals(query.getClassifyType()) && ObjectUtils.isNotNull(query.getFactoryArea())) {
            CommonClassify commonClassify = commonClassifyService.getOne(new LambdaQueryWrapper<CommonClassify>().eq(CommonClassify::getClassifyType, query.getClassifyType())
                    .eq(CommonClassify::getParentClassifyId, 0).eq(CommonClassify::getName,query.getFactoryArea())

            );
            query.setTopClassifyId(commonClassify.getId());
        }
        return getDataTable(commonClassifyService.listVo(query));
    }

    @GetMapping("/list/bom")
    public TableDataInfo bom() {
        CommonClassifyQuery query = new CommonClassifyQuery();
        query.setIsBomClass(true);
        return getDataTable(commonClassifyService.listVo(query));
    }

    @GetMapping("/type/list")
    public TableDataInfo list() {
        QueryWrapper<CommonClassify> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT classify_type");
        wrapper.isNotNull("classify_type");
        List<String> types = commonClassifyService.list(wrapper).stream().map(CommonClassify::getClassifyType).collect(Collectors.toList());
        return getDataTable(types);
    }

    /**
     * 获取通用树形分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('tools:commonClassify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(commonClassifyService.getById(id));
    }

    /**
     * 新增通用树形分类
     */
    @PreAuthorize("@ss.hasPermi('tools:commonClassify:add')")
    @Log(title = "通用树形分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CommonClassify commonClassify) {
        commonClassifyService.saveDetail(commonClassify);
        return AjaxResult.data(commonClassify);
    }

    /**
     * 修改通用树形分类
     */
    @PreAuthorize("@ss.hasPermi('tools:commonClassify:edit')")
    @Log(title = "通用树形分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonClassify commonClassify) {
        commonClassifyService.saveDetail(commonClassify);
        return AjaxResult.data(commonClassify);
    }

    /**
     * 删除通用树形分类
     */
    @PreAuthorize("@ss.hasPermi('tools:commonClassify:remove')")
    @Log(title = "通用树形分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(commonClassifyService.removeByIds(ids));
    }
}
